package com.bjesc_b2c.app.appraiset.mvp.model.impl;

import android.content.Context;

import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.mvp.model.GetCarForModel;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;
import java.util.List;

import master_bean.CarForBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;


/**
 * Created by lenovo on 2017/4/22.
 */

public class GetCarForModelImpl implements GetCarForModel {
    private CallBack callBack;
    public GetCarForModelImpl(CallBack callBack){
        this.callBack = callBack;
    }

    @Override
    public void getAllDealer(Context context) {
        HashMap<String,Object> build = new MapUtil().build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(context, build).create(IApiActivityRequest.class).getDealer(),
                new BaseSubscriber<TokenBean<List<CarForBean>>>(context) {
                    @Override
                    public void onSuccess(TokenBean<List<CarForBean>> o) {
                        if (callBack != null)
                            callBack.callback(o.getData());
                    }
                    @Override
                    public void onFailure(Throwable e) {
                        if (callBack != null)
                            callBack.loadFailure();
                    }

                    @Override
                    public void onStart() {
                        if (callBack != null)
                            callBack.loadStart();
                    }

                    @Override
                    public void onCompleted() {
                        if (callBack != null)
                            callBack.loadCompleted();
                    }
                });
    }

    @Override
    public void manageDealer(Context context, int type, String keyword, int inventory) {
        HashMap<String,Object> build = new MapUtil()
                .put("type",type)
                .put("keyword",keyword)
                .put("inventory",inventory)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(context, build).create(IApiActivityRequest.class).manageDealer(),
                new BaseSubscriber<TokenBean<List<CarForBean>>>(context) {
                    @Override
                    public void onSuccess(TokenBean<List<CarForBean>> o) {
                        if (callBack != null)
                            callBack.callback(o.getData());
                    }
                    @Override
                    public void onFailure(Throwable e) {
                        if (callBack != null)
                            callBack.loadFailure();
                    }

                    @Override
                    public void onStart() {
                        if (callBack != null)
                            callBack.loadStart();
                    }

                    @Override
                    public void onCompleted() {
                        if (callBack != null)
                            callBack.loadCompleted();
                    }
                });
    }

    @Override
    public void requestCarFor(Context context,String marketID) {
        HashMap<String,Object> build = new MapUtil()
                .put("market_id",marketID)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(context, build).create(IApiActivityRequest.class).getDealer(),
                new BaseSubscriber<TokenBean<List<CarForBean>>>(context) {
                   @Override
                    public void onSuccess(TokenBean<List<CarForBean>> o) {
                       if (callBack != null)
                        callBack.callback(o.getData());
                    }
                    @Override
                    public void onFailure(Throwable e) {
                        if (callBack != null)
                            callBack.loadFailure();
                    }

                    @Override
                    public void onStart() {
                        if (callBack != null)
                            callBack.loadStart();
                    }

                    @Override
                    public void onCompleted() {
                        if (callBack != null)
                            callBack.loadCompleted();
                    }
                });
    }

    @Override
    public void requestDealerforType(Context context, int type,String market_id) {
        HashMap<String,Object> build = new MapUtil()
                .put("type",type)
                .put("market_id",market_id)
                .build();

        RxRequest.request(ShowType.SHOW, RxRequest.getClient(context, build).create(IApiActivityRequest.class).getDealerForType(),
                new BaseSubscriber<TokenBean<List<CarForBean>>>(context) {
                    @Override
                    public void onSuccess(TokenBean<List<CarForBean>> o) {
                        if (callBack != null)
                        callBack.callback(o.getData());
                    }
                    @Override
                    public void onFailure(Throwable e) {
                        if (callBack != null)
                            callBack.loadFailure();
                    }

                    @Override
                    public void onStart() {
                        if (callBack != null)
                            callBack.loadStart();
                    }

                    @Override
                    public void onCompleted() {
                        if (callBack != null)
                            callBack.loadCompleted();
                    }
                });
    }

    public interface CallBack{
        void callback(List<CarForBean> listbean);
        void loadStart();
        void loadCompleted();
        void loadFailure();
    }
}
